package org.uk.splitsbrowser.client.view;

import java.text.DateFormat;
import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.cellview.client.CellList;
import com.google.gwt.user.cellview.client.SimplePager;
import com.google.gwt.cell.client.AbstractCell;
import com.google.gwt.cell.client.Cell.Context;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;

import org.uk.splitsbrowser.client.presenter.OEventService;
import org.uk.splitsbrowser.client.presenter.OEventServiceAsync;
import org.uk.splitsbrowser.shared.OEvent;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.view.client.AsyncDataProvider;
import com.google.gwt.view.client.HasData;
import com.google.gwt.view.client.Range;
import com.google.gwt.user.client.ui.TextBox;

public class EventBrowser extends Composite {

	private static EventBrowserUiBinder uiBinder = GWT.create(EventBrowserUiBinder.class);

	// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

	private static class MyDataProvider extends AsyncDataProvider<OEvent> {
		/**
		 * {@link #onRangeChanged(HasData)} is called when the table requests a
		 * new range of data. You can push data back to the displays using
		 * {@link #updateRowData(int, List)}.
		 */
		@Override
		protected void onRangeChanged(HasData<OEvent> display) {
			// Get the new range.
			final Range range = display.getVisibleRange();

			// Make RPC call to get requested data range
			OEventServiceAsync service = GWT.create(OEventService.class);

			/* Callback object */
			AsyncCallback<List<OEvent>> callback = new AsyncCallback<List<OEvent>>() {
				public void onFailure(Throwable caught) {
					new ServerErrorDialog().show("Error retrieving event data"); 
				}

				public void onSuccess(List<OEvent> result) {
					updateRowData(range.getStart(), result);

				}
			};

			service.queryEvent("", range.getStart(), range.getLength(), callback);
		}
	}

	private MyDataProvider provider;

	private void bindEvents() {
		provider = new MyDataProvider();

		provider.addDataDisplay(this.cellList);
	}

	// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

	@UiField(provided = true)
	CellList<OEvent> cellList = new CellList<OEvent>(new AbstractCell<OEvent>() {
		@Override
		public void render(Context context, OEvent value, SafeHtmlBuilder sb) {
			sb.appendHtmlConstant("<div style=\"border: 5px; font-family: Arial Black; font-size: 14px; color: black;\">");
			sb.appendEscaped(value.getName());
			sb.appendHtmlConstant("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
			sb.appendEscaped(DateTimeFormat.getMediumDateFormat().format(value.getDate()));
			sb.appendHtmlConstant("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
			sb.appendEscaped(value.getClub());
			sb.appendHtmlConstant("<br/>");
			sb.appendHtmlConstant("</div>");
			sb.appendHtmlConstant("<div style=\".eventbrowser2\">");
			sb.appendEscaped(value.getDiscipline().toString());
			sb.appendHtmlConstant("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
			sb.appendEscaped(value.getType().toString());
			sb.appendHtmlConstant("</div>");
		}
	});
	
	  
	
	@UiField
	Button btnSelect;
	@UiField
	Button btnSearch;
	@UiField TextBox textSearch;

	interface EventBrowserUiBinder extends UiBinder<Widget, EventBrowser> {
	}

	public EventBrowser() {
		
		initWidget(uiBinder.createAndBindUi(this));
		bindEvents();
	    SimplePager pager = new SimplePager();
	    pager.setDisplay(cellList);
	}

}
